package com.firefighting.app;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.firefighting.entity.User;
import com.firefighting.service.UserService;
import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;
import com.firefighting.util.StringUtil;
import java.sql.ResultSetMetaData;
/**
 * Servlet implementation class AppLogin
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/AppLogin" })
public class AppLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Autowired
	private UserService userService;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		

		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		
		System.out.println(userName+"........."+password);
		
		
		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		
		if(StringUtil.isEmpty(userName) || StringUtil.isEmpty(password)){
			result_jb.put("status", "false");
			result_jb.put("message", "账号或密码为空");
			out.print(result_jb.toString());
			out.flush();
			out.close();
			return ;
		}

		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
	//	ResultSetMetaData data =null;
		try {
			connection = JdbcUtils.getConnection();
			String sql = "select b.name,b.gender,b.age,b.phone_number,b.enterprise_id,c.name,b.creation_time,a.roleId,d.roleName,a.userId  from users a, user_basic_info b, enterprise c,role d "
					+ "where a.userName=? and a.password=? and a.userName=b.login_id and b.enterprise_id=c.id and a.roleId=d.roleId;";
			ps = connection.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, password);
			rs = ps.executeQuery();
			// data=rs.getMetaData();
		//	System.out.print(data.getColumnCount());
			// System.out.print(data.getColumnName(26));
			 
			if (rs.next()) {
				result_jb.put("status", "true");
				result_jb.put("name", rs.getString(1));
				result_jb.put("gender", rs.getString(2));
				result_jb.put("age", rs.getString(3));
			    result_jb.put("phone_number", rs.getString(4));
				result_jb.put("enterprise_id", rs.getString(5));
			    result_jb.put("enterprise_name", rs.getString(6));				
				result_jb.put("creation_time", rs.getString(7));
				result_jb.put("roleId", rs.getString(8));
				result_jb.put("roleName", rs.getString(9));
				result_jb.put("userId", rs.getString(10));
			}
			else {//异常判断
				result_jb.put("status", "false");
				result_jb.put("message", "账号或密码错误");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}
}
